package com.szt.mobile.inventory.modules.main.ui;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.szt.mobile.inventory.R;
import com.szt.mobile.inventory.app.HandsetApplication;
import com.szt.mobile.inventory.modules.inventory.ui.InventoryActivity;

/**
 * Created by yanzhaodi on 2017/11/15.
 */

public class MainActivity extends Activity {

    private HandsetApplication application;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        findViewById(R.id.tv_main_pandian).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getApplicationContext(), InventoryActivity.class));
            }
        });

        findViewById(R.id.tv_main_setting).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getApplicationContext(), PowerSettingActivity.class));
            }
        });

        application = ((HandsetApplication) getApplication());
    }

    @Override
    protected void onResume() {
        super.onResume();

        if (application.checkServiceAndDevice()) {
            application.device.runRead();
        } else {
            restartService();
        }
    }

    public void restartService() {
        new AlertDialog.Builder(MainActivity.this)
                .setMessage(R.string.msg_restart)
                .setPositiveButton(R.string.msg_button_ok,
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog,
                                                int which) {
                                application.unBindService();
                                android.os.Process.killProcess(android.os.Process.myPid());
                            }
                        }).setNegativeButton(R.string.msg_button_no, null)
                .show();
    }
}
